from flask import Flask
from flask import render_template
import requests
import json
from hushenhangqing.util import trans_format,R
from hushenhangqing.database_test import database_mysql
import re
app = Flask(__name__)
app.jinja_env.auto_reload = True

@app.route('/')
def hello_world():
    return render_template('index.html',name=None)

@app.route('/getgplist/<name>')
def get_gp_list(name='sz000858'):
    getk_line_data_url = 'http://money.finance.sina.com.cn/quotes_service/api/json_v2.php/CN_MarketData.getKLineData?symbol='+name+'&scale=60&ma=no&datalen=1023'
    print(getk_line_data_url)
    res = requests.get(url=getk_line_data_url)
    data = json.loads(res.text)
    result_list = []
    for getlist in data:
        data1 = getlist['day']
        data2 = getlist['open']
        data3 = getlist['close']
        data4 = getlist['low']
        data5 = getlist['high']
        data6 = getlist['volume']
        times = trans_format(data1, '%Y-%m-%d %H:%M:%S', '%Y/%m/%d %H:%M:%S')
        list = [times,data2,data3,data4,data5,data6]
        result_list.append(list)
    return R(result_list,name)
@app.route('/getgp')
@app.route('/getgp/<name>')
def get_one_gup(name='sh601166'):
    getk_line_data_url = 'http://hq.sinajs.cn/list='+name
    res = requests.get(url=getk_line_data_url)
    regex = '"([^"]+)"'
    result = re.findall(regex, res.text)
    split_str = re.split(",", result[0])
    return R(split_str,name)

@app.route('/getlist')
def getgupiao_list():
    i=0
    type=4
    page = 1
    total = 1988 / 80
    while (i < total):
        # getk_line_data_url = 'http://web.juhe.cn:8080/finance/stock/shall?stock=a&page='+str(page)+'&type='+str(type)+'&key=ef7f2b5a8e157c387eb71862c61f2927'
        getk_line_data_url = 'http://web.juhe.cn:8080/finance/stock/szall?stock=a&page='+str(page)+'&type='+str(type)+'&key=ef7f2b5a8e157c387eb71862c61f2927'
        print(getk_line_data_url)
        res = requests.get(url=getk_line_data_url)
        data = json.loads(res.text)
        # 创建sql 语句，并执行
        for insert_data in data['result']['data']:
            print((insert_data['symbol'], insert_data['name'], insert_data['trade'], str(insert_data['pricechange']),str(insert_data['changepercent']),insert_data['buy'],insert_data['sell'],insert_data['settlement'],insert_data['open'],insert_data['high'],insert_data['low'],str(insert_data['volume']),str(insert_data['amount']),insert_data['code'],insert_data['ticktime'],str(insert_data['per']),str(insert_data['pb']),str(insert_data['mktcap']),str(insert_data['nmc']),str(insert_data['turnoverratio']),'2021-10-19'))
            sql = "insert into hs_list (symbol,name,trade,pricechange,changepercent,buy,sell,settlement,open,high,low,volume,amount,code,ticktime,per,pb,mktcap,nmc,turnoverratio,create_date) values ('%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s')"%(insert_data['symbol'], insert_data['name'], insert_data['trade'], str(insert_data['pricechange']),str(insert_data['changepercent']),insert_data['buy'],insert_data['sell'],insert_data['settlement'],insert_data['open'],insert_data['high'],insert_data['low'],str(insert_data['volume']),str(insert_data['amount']),insert_data['code'],insert_data['ticktime'],str(insert_data['per']),str(insert_data['pb']),str(insert_data['mktcap']),str(insert_data['nmc']),str(insert_data['turnoverratio']),'2021-10-19')
            print(sql)
            db = database_mysql(database_mysql.db_config)
            db.save_row(sql)

        i = i + 1
        page = page +1
    return R("成功","")

@app.route("/getsh")
@app.route("/getsh/<code>")
def get_sh(code = ''):
    sql = "select  code,name,trade,symbol,id   from hs_list where symbol like 'sh"+str(code)+"%' order by code"
    db = database_mysql(database_mysql.db_config)
    data = db.get_row(sql)
    return R(data,"")

@app.route("/getsz")
@app.route("/getsz/<code>")
def get_sz(code = ''):
    sql = "select  code,name,trade,symbol,id  from hs_list where symbol like 'sz"+str(code)+"%' order by code"
    db = database_mysql(database_mysql.db_config)
    data = db.get_row(sql)
    return R(data, "")

@app.route("/getall")
@app.route("/getall/<code>")
def get_all(code = ''):
    sql = "select  code,name,trade,symbol,id from hs_list where code like '"+str(code)+"%' order by code"
    db = database_mysql(database_mysql.db_config)
    data = db.get_row(sql)
    return R(data, "")


@app.route("/getMe")
@app.route("/getMe/<code>")
def get_me(code = ''):
    sql = "select hs.*,me.createdName from me_list me left join hs_list hs on hs.id = me.hs_id"
    db = database_mysql(database_mysql.db_config)
    data = db.get_row(sql)
    return R(data, "")

@app.route("/saveMe")
@app.route("/saveMe/<hsid>")
def save_me(hsid = ''):
    sql = "insert into me_list(hs_id,createdName) values ('%s','%s')" %(hsid,'王')
    print(sql)
    db = database_mysql(database_mysql.db_config)
    db.save_row(sql)
    return R("true", "")

@app.route("/delete")
@app.route("/delete/<hsid>")
def delete(hsid = ''):
    sql = "delete from me_list where hs_id="+hsid
    print(sql)
    db = database_mysql(database_mysql.db_config)
    db.save_row(sql)
    return R("true", "")



if __name__ == '__main__':
    app.run(host='127.0.0.1',port=5000)